﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Pivotbrowser.Helpers
{
    public class PropertiesParser
    {
        private string oldString { get; set; }
        private Dictionary<string, List<string>> oldItems { get; set; }

        public PropertiesParser(string filter)
        {
            oldString = filter;
            oldItems = Parse(filter);
        }

        public Tuple<Dictionary<string, List<string>>, Dictionary<string, List<string>>> GetChanges(string newfilter)
        {
            var newItems = Parse(newfilter);


            var addedItems = new Dictionary<string, List<string>>(newItems);
            foreach (var key in newItems.Keys)
            {
                if ( oldItems.ContainsKey(key) )
                {
                    List<string> difference = newItems[key].Except(oldItems[key]).ToList<string>();
                    if (difference.Count > 0)
                        addedItems[key] = difference;
                    else
                        addedItems.Remove(key);
                }
            }

            var removedItems = new Dictionary<string, List<string>>(oldItems);
            foreach (var key in oldItems.Keys)
            {
                if (newItems.ContainsKey(key))
                {
                    List<string> difference = oldItems[key].Except(newItems[key]).ToList<string>();
                    if (difference.Count > 0)
                        removedItems[key] = difference;
                    else
                        removedItems.Remove(key);
                }
            }

            oldItems = newItems;

            return new Tuple<Dictionary<string, List<string>>, Dictionary<string, List<string>>>(addedItems, removedItems);
        }

        public Dictionary<string, List<string>> Parse(string filter)
        {
            var result = new Dictionary<string, List<string>>();
            if (filter == null || filter == "" )
                return result;

            filter = filter + "&!";
            while (filter != ("!"))
            {
                var firstEqualAt = filter.IndexOf('=');
                var firstAmpAt = filter.IndexOf('&');
                var firstProperty = filter.Substring(0, firstEqualAt);
                var firstValue = filter.Substring(firstEqualAt + 4, firstAmpAt - (firstEqualAt + 4));

                if (result.ContainsKey(firstProperty))
                {
                    result[firstProperty].Add(firstValue);
                }
                else
                {
                    result.Add(firstProperty, new List<string>() { firstValue });
                }             
                
                filter = filter.Substring(firstAmpAt + 1);
            }

            return result;
        }
    }
}
